<?php

namespace Home\Controller;

/**
 * 店铺类
 */

class BaseStoreController extends BaseMallController {
    
    function _initialize() {
        parent::_initialize();
        
    }
    
    
    function get_store_data(){
        $store_info = D('store')->get_info($store_id);
        if (empty($store_info)) {
            render('10200', '店铺不存在');
        }
        $store['evaluation_average'] = round(($store['evaluation_desc']+$store['evaluation_service']+$store['evaluation_speed'])/3,1);
        //获取店铺评分
        $evaluation = M('OrderGoods')->alias('og')->field('COUNT(*) AS count,SUM(evaluation_service) AS evaluation_service,SUM(evaluation_speed) AS evaluation_speed,SUM(evaluation_desc) AS evaluation_desc')->join('LEFT JOIN ' . C('DB_PREFIX') . 'order o ON o.order_id=og.order_id')->where('seller_id=' . $store_id . ' AND evaluation_status = 1 AND is_valid = 1')->find();
        $stores_avg = M('Store')->field('AVG(evaluation_desc) as avg_evaluation_desc,AVG(evaluation_service) as avg_evaluation_service,AVG(evaluation_speed) as avg_evaluation_speed')->where('state=1')->find();
        if (is_array($evaluation)) {
            $store_info['evaluation'] = array();
            $store_info['evaluation']['evaluation_desc'] = array('average_score' => round($evaluation['evaluation_desc'] / $evaluation['count'], 1));
            $store_info['evaluation']['evaluation_speed'] = array('average_score' => round($evaluation['evaluation_speed'] / $evaluation['count'], 1));
            $store_info['evaluation']['evaluation_service'] = array('average_score' => round($evaluation['evaluation_service'] / $evaluation['count'], 1));

            if (is_array($stores_avg)) {
                if ($store_info['evaluation']['evaluation_service']['average_score'] > $stores_avg['avg_evaluation_service']) {
                    $store_info['evaluation']['evaluation_service']['state'] = 'over';
                }
                if ($store_info['evaluation']['evaluation_desc']['average_score'] > $stores_avg['avg_evaluation_desc']) {
                    $store_info['evaluation']['evaluation_desc']['state'] = 'over';
                }
                if ($store_info['evaluation']['evaluation_speed']['average_score'] > $stores_avg['avg_evaluation_speed']) {
                    $store_info['evaluation']['evaluation_speed']['state'] = 'over';
                }

                if ($store_info['evaluation']['evaluation_service']['average_score'] < $stores_avg['avg_evaluation_service']) {
                    $store_info['evaluation']['evaluation_service']['state'] = 'lower';
                }
                if ($store_info['evaluation']['evaluation_desc']['average_score'] < $stores_avg['avg_evaluation_desc']) {
                    $store_info['evaluation']['evaluation_desc']['state'] = 'lower';
                }
                if ($store_info['evaluation']['evaluation_speed']['average_score'] < $stores_avg['avg_evaluation_speed']) {
                    $store_info['evaluation']['evaluation_speed']['state'] = 'lower';
                }

                if ($store_info['evaluation']['evaluation_service']['average_score'] == $stores_avg['avg_evaluation_service']) {
                    $store_info['evaluation']['evaluation_service']['state'] = 'normal';
                }
                if ($store_info['evaluation']['evaluation_desc']['average_score'] == $stores_avg['avg_evaluation_desc']) {
                    $store_info['evaluation']['evaluation_desc']['state'] = 'normal';
                }
                if ($store_info['evaluation']['evaluation_speed']['average_score'] == $stores_avg['avg_evaluation_speed']) {
                    $store_info['evaluation']['evaluation_speed']['state'] = 'normal';
                }

                $store_info['evaluation']['evaluation_desc']['percent'] = (round(abs($store_info['evaluation']['evaluation_desc']['average_score'] - $stores_avg['avg_evaluation_desc']) / $stores_avg['avg_evaluation_desc'], 4) * 100) . "%";
                $store_info['evaluation']['evaluation_speed']['percent'] = (round(abs($store_info['evaluation']['evaluation_speed']['average_score'] - $stores_avg['avg_evaluation_speed']) / $stores_avg['avg_evaluation_speed'], 4) * 100) . "%";
                $store_info['evaluation']['evaluation_service']['percent'] = (round(abs($store_info['evaluation']['evaluation_service']['average_score'] - $stores_avg['avg_evaluation_service']) / $stores_avg['avg_evaluation_service'], 4) * 100) . "%";
            }
        }
        return $store_info;
    }
    
    
    
}




?>
